<?php
include_once "com/code/Model.php";

class LoginModel extends Model {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function __destruct() {
		parent::__destruct();
	}
	
	public function getUser($login, $password, $publicKey) {
		$connection = mysql_connect("localhost", "root", "3lm4rc3l0");
		mysql_set_charset('utf8', $connection);
		mysql_select_db("visual", $connection);
		
		$login = mysql_real_escape_string(trim($login), $connection);
		$password = mysql_real_escape_string(trim($password), $connection);
		
		$query =  " SELECT * ";
		$query .= " FROM User ";
		$query .= " WHERE User.login = '$login' ";
		$query .= " AND User.validated = TRUE ";
		
		$result = mysql_query($query, $connection) or die(mysql_error());
		
		$user = mysql_fetch_object($result);
		
		mysql_free_result($result);
		mysql_close($connection);
		
		if ($user) {
			$userEncriptedPassword = md5($user->password.$publicKey);
			$user = ($userEncriptedPassword == $password) ? $user : false;
		}
		
		return $user;
	}
}
?>